import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { employeeMentAdd, employeeMentQueryOne, employeeMentEdit } from '../../api/employeeMent';

export function useDetailData(page) {
  const router = useRouter();
  const formItems = [
    {
      label: '员工姓名',
      key: 'name',
      type: 'el-input',
      width: 400,
      rules: [
        { required: true, message: '请填写员工姓名', trigger: 'blur' },
      ],
    },
    {
      label: '用户名',
      key: 'username',
      type: 'el-input',
      width: 400,
      rules: [
        { required: true, message: '请填写用户名', trigger: 'blur' },
      ],
    },
    {
      label: '手机号',
      key: 'phone',
      type: 'el-input',
      width: 400,
      rules: [
        { required: true, message: '请填写手机号', trigger: 'blur' },
      ],
    },
    {
      label: '性别',
      key: 'sex',
      type: 'el-radio',
      radioOptions: [
        {
          label: '男',
          value: '0',
        },
        {
          label: '女',
          value: '1',
        },
      ],
    },
    {
      label: '身份证号码',
      key: 'idCard',
      type: 'el-input',
      width: 400,
      rules: [
        { required: true, message: '请填写身份证信息', trigger: 'blur' },
      ],
    },
  ];
  const formProps = reactive({
    model: {},
    formItems,
  });
  // 新增员工 / 或者编辑
  async function submitData() {
    page.value === 'add' ? await employeeMentAdd(formProps.model) : await employeeMentEdit({ ...formProps.model, id: router.currentRoute.value.query?.id });
    router.go(-1);
  }
  async function saveMore() {
    await employeeMentAdd(formProps.model);
    for (const key in formProps.model) {
      formProps.model[key] = null;
    }
  }
  // 获取单个详情
  async function getEmployeeDetail(id) {
    const { data: { data } } = await employeeMentQueryOne(id);
    for (const key in data) {
      formProps.model[key] = data[key];
    }
  }
  return {
    formProps,
    submitData,
    getEmployeeDetail,
    saveMore,
  };
}
